package com.tanhua.server.controller;

import com.tanhua.model.dto.AnswerDto;
import com.tanhua.model.vo.QuestionnaireVo;
import com.tanhua.model.vo.TestIdentificationVo;
import com.tanhua.server.interceptor.UserHolder;
import com.tanhua.server.service.TestSoulService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 灵魂测试web层
 *
 * @author 李磊
 * @since 2022-01-13 14:17
 */
@RestController
@RequestMapping("/testSoul")
public class TestSoulController {

    @Autowired
    TestSoulService testSoulService;

    /**
     * 说明:  灵魂测试-答题
     *
     * @param answers 测试结果封装
     * @return org.springframework.http.ResponseEntity<java.lang.String>
     * @author 李磊
     * @since 2022-01-13 14:34
     */
    @PostMapping
    public ResponseEntity<String> testSoul(@RequestBody Map<String,List<AnswerDto>> answers) {
        Long userId = UserHolder.getUserId();

        Long reportId = testSoulService.testSoul(answers.get("answers"), userId);
        return ResponseEntity.ok(reportId.toString());

    }

    /**
     * 说明:  查询测试试卷列表
     *
     * @return org.springframework.http.ResponseEntity<java.lang.String>
     * @author 李磊
     * @since 2022-01-13 14:38
     */
    @GetMapping
    public ResponseEntity<List<QuestionnaireVo>> findTestList() {
        Long userId = UserHolder.getUserId();
        List<QuestionnaireVo> questionnaireVoList = testSoulService.findTestList(userId);
        return ResponseEntity.ok(questionnaireVoList);
    }

    /**
     * 说明: 灵魂测试，查询结果
     *
     * @param id 报告id
     * @return org.springframework.http.ResponseEntity<java.lang.Object>
     * @author 李磊
     * @since 2022-01-13 14:43
     */
    @GetMapping("/report/{id}")
    public ResponseEntity<TestIdentificationVo> testReport(@PathVariable Long id) {
        Long userId = UserHolder.getUserId();
        TestIdentificationVo vo = testSoulService.findTestResult(userId, id);
        return ResponseEntity.ok(vo);
    }
}
